﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNet.Common.Command;
using NHibernate;
using System.IO;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using NHibernate.Transform;
using System.Collections;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Member
{
    public partial class ScoreList : System.Web.UI.Page
    {
        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected DateTime Start { get; set; }

        protected DateTime End { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            
            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

            try
            {
                Start = DateTime.Parse(Request["start"]);
                End = DateTime.Parse(Request["end"]);
            }
            catch (System.Exception ex)
            {
            	Start = DateTime.Now.AddDays(-7);
                End = DateTime.Now;
            }


            string hql = "from MemberScoreLog l ,MemberInfo m  where   m.AccountId=l.MemberAccount  and  l.OptTime>:start and l.OptTime<:end ";
            string account = Request["acc"];
            if (!string.IsNullOrEmpty(account))
            {
                    hql += " and l.MemberAccount=:acc";
            }
            hql += "  order by l.OptTime Desc ";
            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {

                IQuery query = session.CreateQuery("select count(*) " + hql).SetParameter("start", Start.Date).SetParameter("end", End.AddDays(1).Date);
                if (!string.IsNullOrEmpty(account))
                {
                    query.SetParameter("acc", account);
                }
                string sel = " select l.Id as id,l.MemberAccount as MemberAccount,m.MemberName as MemberName ,m.PhotoUrl as PhotoUrl,l.OptTime as OptTime,l.Score as Score,l.FinalScore as FinalScore ,l.Remark as Remark ";
                //设置记录总数
                _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());
                    
                //判断是否导出EXCEL操作
                if (!string.IsNullOrEmpty(Request["exportexcel"]))
                {
                    //设置数据源
                    query = session.CreateQuery(sel + hql)
                        .SetFirstResult((pageIndex - 1) * pageSize)
                        .SetMaxResults(pageSize).SetParameter("start", Start.Date).SetParameter("end", End.AddDays(1).Date);
                    if (!string.IsNullOrEmpty(account))
                    {
                        query.SetParameter("acc", account);
                    }
                    IList<IDictionary> list = query.SetResultTransformer(Transformers.AliasToEntityMap).List<IDictionary>();
                    StringWriter sw = new StringWriter();
                    sw.WriteLine("帐号\t记录时间\t积分值\t总积分\t说明");
                    foreach (var item in list)
                    {
                        sw.WriteLine("'" + item["MemberAccount"] + "\t" + item["OptTime"] + "\t" + item["Score"] + "\t"
                             + item["FinalScore"] + "\t" + item["Remark"]);
                    }
                    sw.Close();

                    OutputKit.OutputExcel(Response, sw);
                }
                else
                {
                    //设置数据源
                        query = session.CreateQuery(sel+hql)
                        .SetFirstResult((pageIndex - 1) * pageSize)
                        .SetMaxResults(pageSize).SetParameter("start", Start.Date).SetParameter("end", End.AddDays(1).Date);
                    if (!string.IsNullOrEmpty(account))
                    {
                        query.SetParameter("acc", account);
                    }
                    repeater1.DataSource = query.SetResultTransformer(Transformers.AliasToEntityMap).List();
                    repeater1.DataBind();

                }
            }

        }

    }
}